﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace VIT.Pre
{
    public class ListView : System.Web.UI.WebControls.ListView
    {
        const string DATAPAGERNAME = "DataPager1";
        const int PAGESIZEDATAPAGER = 15;

        void ListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            SelectedIndex = -1;
            InsertItemPosition = System.Web.UI.WebControls.InsertItemPosition.None;
            EditIndex = -1;
            SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            // My Events
            PagePropertiesChanging += new EventHandler<PagePropertiesChangingEventArgs>(ListView_PagePropertiesChanging);
            //----------
        }

        public override void DataBind()
        {
            if (DelegateDataSource != null)
            {
                int pageSize = PAGESIZEDATAPAGER;
                var dataPager = FindControl(DATAPAGERNAME) as DataPager;
                if (dataPager != null) pageSize = dataPager.PageSize;
                DataSource = WSGlobal.WSCollection(DelegateDataSource, pageSize);
            }
            base.DataBind();
        }

        public WSEnumerableDelegate DelegateDataSource { get; set; }
    }
}